|
|
|
|
|
|
|
The GetCLSIDAsString Method |
|
|
|
|
|
|
|
|
The following code can be used to convert the LPOLESTR string pointer provided by the StringFromCLSID function into a Visual Basic string: |
|
|
|
|
|
|
|
|
Private Declare Function lstrlenW Lib "kernel32" _
(ByVal lpString As Long) As Long
Private Declare Function RtlMoveMemory Lib "kernel32" _
(dest As Any, source As Any, ByVal count As Long) As Long
Public Function GetCLSIDAsString() As String
Dim cid As CLSID
Dim StringBuffer() As Byte
Dim StringLength As Long
cid = GetCLSID()
Dim MemoryPointer As Long
Call StringFromCLSID(cid, MemoryPointer)
StringLength = lstrlenW(MemoryPointer) * 2
ReDim StringBuffer(StringLength)
Call RtlMoveMemory(StringBuffer(0), ByVal MemoryPointer, _
StringLength)
GetCLSIDAsString = StringBuffer()
End Function |
|
|
|
|
|
|
|
|
The MemoryPointer variable is a Long variable that is loaded by the StringFromCLSID function with the address of the LPOLESTR string. The first step in converting this string into a Visual Basic string is to find out how long it is. This can be accomplished using the lstrlenW function. The lstrlen API function returns the number of characters in a string. It has two entry points: lstrlenA for ANSI strings and lstrlenW for Unicode strings. Since the MemoryPointer variable will contain the address of a Unicode string, the lstrLenW function is the right one to use. The lpString parameter of the lstrlenW function is declared ByVal As Long because you are passing the address of the string data, which in this case is contained in the Long MemoryPointer variable. |
|
|
|
|
|
|
|
|
The number of bytes in the string is twice the length of the string. Remember, the lstrlenW function returns the number of characters in the string, and Unicode strings use 2 bytes for each character. The function dimensions a byte array to the size necessary to hold the Unicode string data. The RtlMoveMemory function is then used to copy the text from the MemoryPointer buffer to the new StringBuffer array. Finally, the function takes advantage of Visual Basic's ability to directly assign an array to a string to copy the contents of |
|
|
|
|
|